package com.powernode.ssm.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.powernode.ssm.pojo.Account;
import com.powernode.ssm.result.CodeEnum;
import com.powernode.ssm.result.R;
import com.powernode.ssm.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class SsmController {

    @Autowired
    private AccountService accountService;

    @GetMapping("/all")
    public List<Account> all(){
        List<Account> accounts = accountService.queryAll();
        return accounts;
    }

    @GetMapping("/page/{pageNo}")
    public PageInfo<Account> page(@PathVariable("pageNo") int pageNo){
        PageHelper.startPage(pageNo,3);
        List<Account> accounts = accountService.queryAll();
        PageInfo<Account> accountPageInfo = new PageInfo<>(accounts);
        return accountPageInfo;
    }

    @GetMapping("/pageR/{pageNo}")
    public R pageR(@PathVariable("pageNo") int pageNo){
        PageHelper.startPage(pageNo,3);
        List<Account> accounts = accountService.queryAll();
        PageInfo<Account> accountPageInfo = new PageInfo<>(accounts);
        return R.OK(accountPageInfo);
    }

    @GetMapping("/fail")
    public R fail(){

        return R.Fail(CodeEnum.NOT_FOUND);
    }

    @GetMapping("/one")
    public Account one(){
        return new Account(1,"text",100D);
    }
}
